package com.css.util;

import java.security.SecureRandom;
import java.util.Base64;

public class SecKeyGenerator {
    public static String generateSk() {
        return  generateSk("sk-", 32);
    }
    public static String generateUk(){
        return generateSk("uk-", 32);
    }
    // 生成随机 API 密钥（格式示例：sk-5Fj8Lp3QxR9zWbY2vKd7HtN4mGc6V）
    public static String generateSk(String prefix, int length) {
        SecureRandom random = new SecureRandom();
        String characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
        StringBuilder sb = new StringBuilder();

        // 生成随机字符
        for (int i = 0; i < length; i++) {
            int index = random.nextInt(characters.length());
            sb.append(characters.charAt(index));
        }

        return prefix + sb;
    }

    public static String generateBase64Key(String prefix, int byteLength) {
        SecureRandom random = new SecureRandom();
        byte[] bytes = new byte[byteLength];
        random.nextBytes(bytes);
        String encodedKey = Base64.getUrlEncoder().withoutPadding().encodeToString(bytes);
        return prefix + encodedKey;
    }

    public static void main(String[] args) {
        System.out.println("Generated API Key: " + generateSk());
    }
}
